package tron.oauth.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import tron.oauth.config.RootService;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/tron")
public class TronController {

    @Autowired
    RootService rootService;

    @Autowired
    WebApplicationContext webApplicationContext;


    @GetMapping("/getUser")
    public Object getUser() {
        return "";
    }

    @GetMapping("/tron")
    public Object test(String a,String b) {
        rootService.getUserByToken();
        RequestMappingHandlerMapping mapping = webApplicationContext.getBean(RequestMappingHandlerMapping.class);

        //获取url与类和方法的对应信息
        Map<RequestMappingInfo, HandlerMethod> map = mapping.getHandlerMethods();
        List<String> urls = new ArrayList<>();

        StringBuilder stringBuilder = new StringBuilder();
        try {
            final FileReader fileReader = new FileReader(new File("./spring-oauth/src/main/java/tron/oauth/LookMe.java"));
            final BufferedReader bufferedReader = new BufferedReader(fileReader);
            final String s = bufferedReader.readLine();
            String temp;
            while (null != (temp = bufferedReader.readLine())) {
                if (temp.contains("@url")) {
                    stringBuilder.append(temp);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


        map.forEach((k,v)->{
            String path = k.getPatternsCondition().getPatterns().toArray()[0] + "";
            if (!stringBuilder.toString().contains(path)) {
            System.out.println("/**");
            System.out.println("* {@url "+ path + "}");
            System.out.println("* {@link " + v.getShortLogMessage().replace("["," [") + "}");
            System.out.println("*/");
            System.out.println();
            }
        });
        return stringBuilder;

    }

}
